package {
	import bonus.Bonus;
	import field.GamePanel;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.events.SecurityErrorEvent;
	import flash.system.Security;
	import gui.ShowVoices;
	import vk.*;
	/**
	 * ...
	 * @author 
	 */
	public class Main extends MovieClip
	{
		static public const SERVER_ADDRESS = "http://pay55xx.apexpointsoft.com/";
		public var vKontakte: APIConnection = null;
		public var viewerID: Number = 0;
		
		public var scores: Scores = null;	//Visually declared
		public var level: Level = null;
		public var currentBonus: Bonus = null;
		public var panel: GamePanel = null;
		public var votes: VoicesDisplayer = null;
		
		public var localWork: Boolean = false;
		
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public var tf: TextField = new TextField();
		private function init(e:Event = null):void 
		{			
			//TODO: Application should ask user about adding into application list
			//Security.loadPolicyFile(SERVER_ADDRESS + "crossdomain.xml");
			var flashVars: Object = stage.loaderInfo.parameters as Object;
			
			//Local testing
			
			tf.text = "Test\nmulti\nline";
			
			if (!flashVars.api_id) {
				flashVars['api_id'] = "2415458"
				flashVars['viewer_id'] = "75041521"
				flashVars['sid'] = "ff0072ab5b0d195866ab2e0212bc25c05861b92af550a678b255e14d0c8d99"
				flashVars['secret'] = "61f0323b23"
				
				localWork = true;
			}else {
				tf.text = "flashVars['api_id'] = \"" + flashVars['api_id'] + "\"\n" +
				"flashVars['viewer_id'] = \"" + flashVars['viewer_id'] + "\"\n" +
				"flashVars['sid'] = \"" + flashVars['sid'] + "\"\n" +
				"flashVars['secret'] = \"" + flashVars['secret'] + "\"\n";	
			}
			
			if( flashVars['viewer_id'] == 75041521)
				this.addChild(tf);
			viewerID = flashVars['viewer_id'];
			
			vKontakte = new APIConnection(flashVars);
			//vKontakte.addEventListener("onConnectionInit", onVkComplete);
			//vKontakte.api("friends.get", { uids: flashVars['viewer_id'], fields: "photo" }, onVkComplete, onVkError);
			
			//var mc: MovieClip = new MovieClip;
			
			onBalanceChanged(null);
			if (!localWork){
				vKontakte.api ("getUserSettings", { }, askAccess, onVkError);
				vKontakte.addEventListener("onBalanceChanged", onBalanceChanged);
			}
							
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			// entry point
		}
		
		private function onSecure(e: SecurityErrorEvent) {
			tf.text += "Sosnooley";
		}
		
		private function onComplete(e: Event): void {
			tf.text = tf.text + "Incoming message";
			trace (URLLoader(e.target).data);
		}
		private function onBalanceChanged(value: Object) {
			tf.text = tf.text + "Incoming message";
			var loader: URLLoader = new URLLoader ();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecure);
			loader.load(new URLRequest(SERVER_ADDRESS + "index.php?uid=" + viewerID));
		}
		
		private function askAccess(e: Object) {
			if (! ( Number(e) & 2) ) { 	//If we don't have permissions to see friends
				vKontakte.callMethod("showSettingsBox", 2);
			//}
		}
		}
		public function onVkComplete(e: Object): void {
			//trace(e as String);
		/*	for (var s in e) {
				trace(s, '=', e[s]);
			}*/
			
			var x: Number = 128;
			
			for each (var o: Object in e) {
				x += 64;
				var imageLoader: Loader = new Loader();
				imageLoader.x = x;
				imageLoader.y = 0;
				
				imageLoader.load(new URLRequest(o.photo));
				
				var cmf: ColorMatrixFilter = new ColorMatrixFilter();
				cmf.matrix = [1, 0, 0, 0, 0,
							 0, 1, 0, 0, 0,
							0, 0, 1, 0, 0,
							0, 0, 0, 1, 0];
				imageLoader.filters = [cmf];
				this.addChild(imageLoader);
			}
			
			
			
			//for (var s: String in e) {
//				trace(s + "=>" + e[s]);
			//}
		}
		
		public function onVkError(e: Object): void {
			var o: MovieClip = new MovieClip();
			o.someProperty = 1;
			//trace(e);
		}
	}
	
	
	
}